package party.loveit.download.core;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.List;
import java.util.Vector;

import static party.loveit.download.core.DLCons.DBCons.TB_TASK;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_CURRENT_BYTES;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_DIR_PATH;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_DISPOSITION;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_DL_STATE;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_ETAG;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_FILE_NAME;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_LOCATION;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_MIME_TYPE;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_TOTAL_BYTES;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_URL_BASE;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_URL_IMG;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_URL_REAL;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_VIDEO_NAME;
import static party.loveit.download.core.DLCons.DBCons.TB_TASK_VIDEO_TIME;

class TaskDAO implements ITaskDAO {
    private final DLDBHelper dbHelper;

    TaskDAO(Context context) {
        dbHelper = new DLDBHelper(context);
    }

    @Override
    public void insertTaskInfo(DLInfo info) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL("INSERT INTO " + TB_TASK + "(" +
                        TB_TASK_URL_BASE + ", " +
                        TB_TASK_URL_REAL + ", " +
                        TB_TASK_DIR_PATH + ", " +
                        TB_TASK_FILE_NAME + ", " +
                        TB_TASK_MIME_TYPE + ", " +
                        TB_TASK_ETAG + ", " +
                        TB_TASK_DISPOSITION + ", " +
                        TB_TASK_LOCATION + ", " +
                        TB_TASK_CURRENT_BYTES + ", " +
                        TB_TASK_URL_IMG + ", " +
                        TB_TASK_DL_STATE + ", " +
                        TB_TASK_VIDEO_NAME + ", " +
                        TB_TASK_VIDEO_TIME + ", " +
                        TB_TASK_TOTAL_BYTES + ") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                new Object[]{info.baseUrl, info.realUrl, info.dirPath, info.fileName,
                        info.mimeType, info.eTag, info.disposition, info.location,
                        info.currentBytes, info.imgUrl, info.dlState, info.videoName, info.videoTime, info.totalBytes});
        db.close();
    }

    @Override
    public void deleteTaskInfo(String url) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL("DELETE FROM " + TB_TASK + " WHERE " + TB_TASK_URL_BASE + "=?",
                new String[]{url});
        db.close();
    }

    @Override
    public void updateTaskInfo(DLInfo info) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL("UPDATE " + TB_TASK + " SET " +
                TB_TASK_DISPOSITION + "=?," +
                TB_TASK_LOCATION + "=?," +
                TB_TASK_MIME_TYPE + "=?," +
                TB_TASK_TOTAL_BYTES + "=?," +
                TB_TASK_FILE_NAME + "=?," +
                TB_TASK_DL_STATE + "=?," +
                TB_TASK_CURRENT_BYTES + "=? WHERE " +
                TB_TASK_URL_BASE + "=?", new Object[]{info.disposition, info.location,
                info.mimeType, info.totalBytes, info.fileName, info.dlState, info.currentBytes, info.baseUrl});
        db.close();
    }

    @Override
    public DLInfo queryTaskInfo(String url) {
        DLInfo info = null;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT " +
                TB_TASK_URL_BASE + ", " +
                TB_TASK_URL_REAL + ", " +
                TB_TASK_DIR_PATH + ", " +
                TB_TASK_FILE_NAME + ", " +
                TB_TASK_MIME_TYPE + ", " +
                TB_TASK_ETAG + ", " +
                TB_TASK_DISPOSITION + ", " +
                TB_TASK_LOCATION + ", " +
                TB_TASK_CURRENT_BYTES + ", " +
                TB_TASK_URL_IMG + ", " +
                TB_TASK_DL_STATE + ", " +
                TB_TASK_VIDEO_NAME + ", " +
                TB_TASK_VIDEO_TIME + ", " +
                TB_TASK_TOTAL_BYTES + " FROM " +
                TB_TASK + " WHERE " +
                TB_TASK_URL_BASE + "=?", new String[]{url});
        if (c.moveToFirst()) {
            info = new DLInfo();
            info.baseUrl = c.getString(0);
            info.realUrl = c.getString(1);
            info.dirPath = c.getString(2);
            info.fileName = c.getString(3);
            info.mimeType = c.getString(4);
            info.eTag = c.getString(5);
            info.disposition = c.getString(6);
            info.location = c.getString(7);
            info.currentBytes = c.getLong(8);
            info.imgUrl = c.getString(9);
            int state = c.getInt(10);
            if(state != DLStates.DL_COMPLETE)
                state = DLStates.DL_NORMAL;
            info.dlState = state;
            info.videoName = c.getString(11);
            info.videoTime = c.getString(12);
            info.totalBytes = c.getLong(13);
        }
        c.close();
        db.close();
        return info;
    }

    @Override
    public List<DLInfo> queryAllTaskInfo() {
        List<DLInfo> infos = new Vector<>();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT " +
                TB_TASK_URL_BASE + ", " +
                TB_TASK_URL_REAL + ", " +
                TB_TASK_DIR_PATH + ", " +
                TB_TASK_FILE_NAME + ", " +
                TB_TASK_MIME_TYPE + ", " +
                TB_TASK_ETAG + ", " +
                TB_TASK_DISPOSITION + ", " +
                TB_TASK_LOCATION + ", " +
                TB_TASK_CURRENT_BYTES + ", " +
                TB_TASK_URL_IMG + ", " +
                TB_TASK_DL_STATE + ", " +
                TB_TASK_VIDEO_NAME + ", " +
                TB_TASK_VIDEO_TIME + ", " +
                TB_TASK_TOTAL_BYTES + " FROM " +
                TB_TASK , null);
        while (c.moveToNext()) {
            DLInfo info = new DLInfo();
            info.baseUrl = c.getString(0);
            info.realUrl = c.getString(1);
            info.dirPath = c.getString(2);
            info.fileName = c.getString(3);
            info.mimeType = c.getString(4);
            info.eTag = c.getString(5);
            info.disposition = c.getString(6);
            info.location = c.getString(7);
            info.currentBytes = c.getLong(8);
            info.imgUrl = c.getString(9);
            int state = c.getInt(10);
            if(state != DLStates.DL_COMPLETE)
                state = DLStates.DL_NORMAL;
            info.dlState = state;
            info.videoName = c.getString(11);
            info.videoTime = c.getString(12);
            info.totalBytes = c.getLong(13);
            infos.add(info);
        }
        c.close();
        db.close();
        return infos;
    }
}